﻿
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System;

public class DBConnection
{
    // string de conexão com o banco
    private string connectionString;

    // Guarda ultima Mensagem de Erro da conexao
    private string messageError;

    // guarda ultima Numero de Erro da conexao
    private int numberError;

    // Indica ULTIMO comando foi executado com sucesso
    private bool isCommandSuccessful;

    // Usado para retornar colecao de registros
    private SqlDataReader rsData;



    public DBConnection()
    {
        connectionString = ConfigurationManager.ConnectionStrings["db_SistemaProntuario"].ConnectionString.ToString();
        numberError = 0;
        messageError = "No errors";
    }

    public DBConnection(string connectionName)
    {
        ConnectionString = connectionName;
        numberError = 0;
        messageError = "No errors";
    }


    public SqlDataReader ExecuteQuery(string myQueryString)
    {
        try
        {             
            if (myQueryString.Length > 0) 
            {
                // Inicia conexão com o banco de dados
                SqlConnection myConnection = new SqlConnection(connectionString);

                // Abre a conexão
                myConnection.Open();
                
                SqlCommand cmd = new SqlCommand(myQueryString, myConnection);
                
                // Executa um comando que retorna dados em um SqlDataReader
                rsData = cmd.ExecuteReader();            
                
                isCommandSuccessful = true;
                myConnection.Close();
                
            }
        }
        catch (Exception ex)
        {
            isCommandSuccessful = false;
            messageError = ex.Message.ToString();       
            numberError = ex.GetHashCode();
        }
        return rsData;
    }

    public string ConnectionString
    {
        get { return connectionString; }
        set 
        {
            if (value.Length == 0)
                connectionString = ConfigurationManager.ConnectionStrings["db_SistemaProntuario"].ConnectionString.ToString();
            else
                connectionString = connectionString = ConfigurationManager.ConnectionStrings[value].ConnectionString.ToString();  
        }
    }

    public SqlDataReader RecordSet
    {        
        get { return rsData; }
    }

    public int ErrorNumber
    {        
        get { return numberError; }
    }

    public string ErrorDescription
    {       
        get { return messageError; }
    }

    public bool IsCommandSuccessful
    {        
        get { return isCommandSuccessful; }
    }

}
